home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / crtdel / crtdel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-15  |  3.2 KB  |  122 lines

  1. /* 
  2.  * crtdel.c --
  3.  *
  4.  *    This program is a stand-alone benchmark that measures
  5.  *    the cost of doing the following:  create a file,
  6.  *    write some bytes to it, close the file, open in read
  7.  *    mode, read all the bytes, close the file, delete the
  8.  *    file.  It should be invoked as follows:
  9.  *
  10.  *    crtdel fileName kBytes count
  11.  *
  12.  *    "fileName" is the name of the file, "kBytes" tells
  13.  *    how many kbytes to write to it, and "count" tells how
  14.  *    many times to repeat the experiment.
  15.  *
  16.  * Copyright 1989 Regents of the University of California.
  17.  * Permission to use, copy, modify, and distribute this
  18.  * software and its documentation for any purpose and without
  19.  * fee is hereby granted, provided that the above copyright
  20.  * notice appear in all copies.  The University of California
  21.  * makes no representations about the suitability of this
  22.  * software for any purpose.  It is provided "as is" without
  23.  * express or implied warranty.
  24.  */
  25.  
  26. #ifndef lint
  27. static char rcsid[] = "$Header: /sprite/src/benchmarks/crtdel/RCS/crtdel.c,v 1.3 89/09/15 11:22:00 brent Exp $ SPRITE (Berkeley)";
  28. #endif not lint
  29.  
  30.  
  31. #include <stdio.h>
  32. #include <sys/file.h>
  33. #include <sys/time.h>
  34. #include <sys/resource.h>
  35.  
  36. static char buffer[16384];
  37.  
  38. main(argc, argv)
  39. int argc;
  40. char **argv;
  41. {
  42.     int cnt, repeats, kBytes, fd, count, i;
  43.     double msPer, micros;
  44.     struct rusage begin ,end;
  45.     struct timeval start, stop;
  46.     struct timezone tz;
  47.  
  48.     if (argc != 4) {
  49.     fprintf(stderr, "Usage:  %s fileName kBytes count\n",
  50.         argv[0]);
  51.     exit(1);
  52.     }
  53.     kBytes = atoi(argv[2]);
  54.     repeats = atoi(argv[3]);
  55.  
  56. #ifdef GETRUSAGE
  57.     getrusage(RUSAGE_SELF, &begin);
  58. #else
  59.     gettimeofday(&start, (struct timezone *) NULL);
  60. #endif
  61.  
  62.     for (count = 0 ; count < repeats; count++) {
  63.     fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0700);
  64.     if (fd < 0) {
  65.         fprintf(stderr, "Couldn't create %s.\n", argv[1]);
  66.         exit(1);
  67.     }
  68.     for (i = kBytes; i > 0; ) {
  69.         if (i > 16) {
  70.         if (write(fd, buffer, 16384) < 0) {
  71.             fprintf(stderr, "Error while writing.\n");
  72.             exit(1);
  73.         }
  74.         i -= 16;
  75.         } else {
  76.         if (write(fd, buffer, 1024*i - 1) < 0) {
  77.             fprintf(stderr, "Error while writing.\n");
  78.             exit(1);
  79.         }
  80.         i = 0;
  81.         }
  82.     }
  83.     if (close(fd) != 0) {
  84.         fprintf(stderr, "Error closing %s after writing.\n",
  85.             argv[1]);
  86.         exit(1);
  87.     }
  88.     fd = open(argv[1], O_RDONLY, 0700);
  89.     if (fd < 0) {
  90.         fprintf(stderr, "Couldn't open %s to read it back.\n",
  91.             argv[1]);
  92.         exit(1);
  93.     }
  94.     while (1) {
  95.         cnt = read(fd, buffer, 16384);
  96.         if (cnt < 16384) break;
  97.     }
  98.     if (close(fd) != 0) {
  99.         fprintf(stderr, "Error closing %s after reading.\n",
  100.             argv[1]);
  101.         exit(1);
  102.     }
  103.     if(unlink(argv[1]) != 0) {
  104.         fprintf(stderr, "Error removing %s.\n", argv[1]);
  105.         exit(1);
  106.     }
  107.     }
  108. #ifdef GETRUSAGE
  109.     getrusage(RUSAGE_SELF, &end);
  110.     micros = (end.ru_utime.tv_sec + end.ru_stime.tv_sec
  111.         - begin.ru_utime.tv_sec - begin.ru_stime.tv_sec)*1000000
  112.         + (end.ru_utime.tv_usec - begin.ru_utime.tv_usec)
  113.         + (end.ru_stime.tv_usec - begin.ru_stime.tv_usec);
  114. #else
  115.     gettimeofday(&stop, (struct timezone *) NULL);
  116.     micros = 1000000*(stop.tv_sec - start.tv_sec)
  117.         + stop.tv_usec - start.tv_usec;
  118. #endif
  119.     msPer = (micros/repeats/1000.0);
  120.     printf("Time per iteration: %.2f milliseconds\n", msPer);
  121. }
  122.